<?php
//Header guard
if(!defined("FORMDIALOG")) {
    define("FORMDIALOG", true);
?>

<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=> 'formDialog',
    // additional javascript options for the dialog plugin
    'options'=>array(
        'autoOpen'=>false,
    ),
));

    echo '<div id="formDialogError" class="error"></div>';
    echo "Name: <br/>";
    echo CHtml::textField("name", "tes", array("id" => "formDialogInput"));
    echo "<br/>";

$this->endWidget('zii.widgets.jui.CJuiDialog');

?>

<script type="text/javascript">

function building_openForCreate(reloadId) {
    $("#formDialogInput").val("");
    $("#formDialogError").html("");

    var click = function(){
        //send data to server
        var url = "<?php echo $this->createUrl("building/update", array("name" => "xreplacex")); ?>";
        $.getJSON(
            url.replace("xreplacex", $("#formDialogInput").val()),
            function(data){
                if(data.errId) {
                    //Handle error
                    alert(data.errId);
                    $("#formDialogError").html(data.errEx);
                } else {
                    //Handle succeed
                    //Close dialog
                    $("#formDialog").dialog('close');

                    //Reload
                    $.ajax({
                        url: "<?php echo $this->createUrl($this->id . '/getaccordion'); ?>",
                        success: function(data, textStatus, jqXHR) {
                            $("#" + reloadId).html(data);
                            $("#accordion").accordion();
                            MAP.refresh();
                        }
                    });
                }
            }
        );
    };

    $("#formDialog").dialog({
        title: 'Create New Building',
        buttons: {
            "Create" : click
        }
    });
    $("#formDialog").dialog("open");
}

function building_openForUpdate(id, name, reloadId) {
    $("#formDialogInput").val(name);
    $("#formDialogError").html("");

    var click = function(){
        //send data to server
        var url = "<?php echo $this->createUrl("building/update", array("name" => "xnamex", "id" => "xidx")); ?>";
        $.getJSON(
            url.replace("xnamex", $("#formDialogInput").val()).replace("xidx", id),
            function(data){
                if(data.errId) {
                    //Handle error
                    $("#formDialogError").html(data.errEx);
                } else {
                    //Handle succeed
                    //Close dialog
                    $("#formDialog").dialog('close');

                    //Reload
                    $.ajax({
                        url: "<?php echo $this->createUrl($this->id . '/getaccordion'); ?>",
                        success: function(data, textStatus, jqXHR) {
                            $("#" + reloadId).html(data);
                            $("#accordion").accordion();
                            MAP.refresh();
                        }
                    });
                }
            }
        );
    };

    $("#formDialog").dialog({
        title: 'Edit Building',
        buttons: {
            "Save" : click
        }
    });
    $("#formDialog").dialog("open");
}

function floor_openForCreate(buildingId, gridId) {
    $("#formDialogInput").val("");
    $("#formDialogError").html("");

    var click = function(){
        //send data to server
        var url = "<?php echo $this->createUrl("floor/update", array("floorNumber" => "xfloorx", "buildingId" => "xbuildingIdx", "imageUrl" => null)); ?>";
        var replaced = url.replace("xfloorx", $("#formDialogInput").val()).replace("xbuildingIdx", buildingId);
        $.getJSON(
            replaced,
            function(data){
                if(data.errId) {
                    //Handle error
                    $("#formDialogError").html(data.errEx);
                } else {
                    //Handle succeed
                    $("#formDialog").dialog('close');
                    $.fn.yiiGridView.update(gridId);
                    MAP.refresh();
                }
            }
        );
    };

    $("#formDialog").dialog({
        title: 'Create New Floor',
        buttons: {
            "Create" : click
        }
    });
    $("#formDialog").dialog("open");
}

function floor_openForUpdate(id, name, gridId) {
    $("#formDialogInput").val(name);
    $("#formDialogError").html("");

    var click = function(){
        //send data to server
        var url = "<?php echo $this->createUrl("floor/update", array("floorNumber" => "xfloorx", "id" => "xidx")); ?>";
        var replaced = url.replace("xfloorx", $("#formDialogInput").val()).replace("xidx", id);
        $.getJSON(
            replaced,
            function(data){
                if(data.errId) {
                    //Handle error
                    $("#formDialogError").html(data.errEx);
                } else {
                    //Handle succeed
                    $("#formDialog").dialog('close');
                    $.fn.yiiGridView.update(gridId);
                    MAP.refresh();
                }
            }
        );
    };

    $("#formDialog").dialog({
        title: 'Edit Floor',
        buttons: {
            "Save" : click
        }
    });
    $("#formDialog").dialog("open");
}

function roomType_openForCreate(gridId) {
    $("#formDialogInput").val("");
    $("#formDialogError").html("");

    var click = function(){
        //send data to server
        var url = "<?php echo $this->createUrl("roomType/update", array("name" => "xreplacex")); ?>";
        $.getJSON(
            url.replace("xreplacex", $("#formDialogInput").val()),
            function(data){
                if(data.errId) {
                    //Handle error
                    $("#formDialogError").html(data.errEx);
                } else {
                    //Handle succeed
                    $("#formDialog").dialog('close');
                    $.fn.yiiGridView.update(gridId);
                }
            }
        );
    };

    $("#formDialog").dialog({
        title: 'Create New Room Type',
        buttons: {
            "Create" : click
        }
    });
    $("#formDialog").dialog("open");
}

function roomType_openForUpdate(id, name, gridId) {
    $("#formDialogInput").val(name);
    $("#formDialogError").html("");

    var click = function(){
        //send data to server
        var url = "<?php echo $this->createUrl("roomType/update", array("name" => "xnamex", "id" => "xidx")); ?>";
        $.getJSON(
            url.replace("xnamex", $("#formDialogInput").val()).replace("xidx", id),
            function(data){
                if(data.errId) {
                    //Handle error
                    $("#formDialogError").html(data.errEx);
                } else {
                    //Handle succeed
                    $("#formDialog").dialog('close');
                    $.fn.yiiGridView.update(gridId);
                }
            }
        );
    };

    $("#formDialog").dialog({
        title: 'Edit Room Type',
        buttons: {
            "Save" : click
        }
    });
    $("#formDialog").dialog("open");
}

</script>

<?php
}
?>